package com.example.finalapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SearchView;
import android.widget.SimpleAdapter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FoodActivity1<MyBean> extends AppCompatActivity implements AdapterView.OnItemClickListener,Runnable {
    private static final String TAG = "FoodActivity1";

    private Handler handler;
    private ListView mylist;
    private SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food1);
        mylist = findViewById(R.id.mylistview);

        searchView = findViewById(R.id.searchView);
        mylist.setTextFilterEnabled(true);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            // 当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String query) {

                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                if (!TextUtils.isEmpty(newText)){
                    mylist.setFilterText(newText);
                }else{
                    mylist.clearTextFilter();
                }
                return false;
            }
        });

        mylist.setOnItemClickListener(this);
        handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                //收到数据
                if (msg.what == 7) {
                    ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) msg.obj;
                    //构造Adapter
                    SimpleAdapter ListItemAdapter = new SimpleAdapter(FoodActivity1.this,
                            list,
                            R.layout.list_item1,
                            new String[]{"cname", "cval"},
                            new int[]{R.id.title, R.id.detail}
                    );
                    mylist.setAdapter(ListItemAdapter);
                }
                super.handleMessage(msg);
            }
        };
        Thread t = new Thread(this);
        t.start();


    }

    public void run() {

        //获取网络数据
        List<HashMap<String, String>> reelist = new ArrayList<>();
        Intent intent = getIntent();
         String location = intent.getStringExtra("location");

         if (location.equals(0)){
             for (int i = 1; i < 11; i++) {
                 try {
                     Log.i(TAG, "run: running......");
                     Document doc = Jsoup.connect("https://www.boohee.com/food/group/1?page="+i).get();
                     Elements lis = doc.getElementsByTag("li");
                     for (Element li : lis) {
                         Elements h4 = li.getElementsByTag("h4");
                         Elements p = li.getElementsByTag("p");
                         if (h4.size() > 0) {
                             String td1 = h4.text();
                             String td2 = p.text();
                             Log.i(TAG, "run: " + td1 + "-->" + td2);
                             HashMap<String, String> map = new HashMap<String, String>();
                             map.put("cname", td1);
                             map.put("cval", td2);
                             reelist.add(map);
                         }
                     }

                 } catch (IOException e) {
                     e.printStackTrace();
                 }

                 Message msg = handler.obtainMessage(7, reelist);
                 handler.sendMessage(msg);
                 Log.i(TAG, "run: sendMessage OK");
             }
         }else {
             int j = Integer.valueOf(location).intValue();
             int j1 = j+1;
             for (int i = 1; i < 11; i++) {
                 try {
                     Log.i(TAG, "run: running......");
                     Document doc = Jsoup.connect("https://www.boohee.com/food/group/"+j1+"?page="+i).get();
                     Log.i(TAG, "run: title=");

                     Elements lis = doc.getElementsByTag("li");
                     for (Element li : lis) {
                         Elements h4 = li.getElementsByTag("h4");
                         Elements p = li.getElementsByTag("p");
                         if (h4.size() > 0) {
                             String td1 = h4.text();
                             String td2 = p.text();
                             Log.i(TAG, "run: " + td1 + "-->" + td2);
                             HashMap<String, String> map = new HashMap<String, String>();
                             map.put("cname", td1);
                             map.put("cval", td2);
                             reelist.add(map);
                         }
                     }

                 } catch (IOException e) {
                     e.printStackTrace();
                 }

                 Message msg = handler.obtainMessage(7, reelist);
                 handler.sendMessage(msg);
                 Log.i(TAG, "run: sendMessage OK");
             }
         }
         }

    private List<MyBean> mListInfo = new ArrayList<>();

    public void updateList(List<MyBean> listInfo){
        mListInfo.clear();
        mListInfo.addAll(listInfo);
        notifyDataSetChanged();
    }

    private void notifyDataSetChanged() {
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}